/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-07-12
 * Time: 11:06
 */
class MyArray<T /*extends Number*/>{//extends Number规定了传入的参数是Number或Number的子类，
                                    // 指定了传入的参数E的一个界限
    private Object[] objects = new Object[10];
    public T getVal(int pos) {
        return (T)objects[pos];
    }
    public void setPos(int pos,T val) {
        this.objects[pos] = val;
    }
}
public class Test1 {
    public static void main(String[] args) {
        Test1 test1 = new Test1();
        System.out.println(test1);//Test1@4eec7777

        MyArray<Integer> myArray = new MyArray<>();
        MyArray<String> myArray1 = new MyArray<>();//T extends Number后 String不是Number的子类
        System.out.println(myArray);//擦除机制 MyArray@41629346
        System.out.println(myArray1);//擦除机制 MyArray@404b9385
    }
    public static void main1(String[] args) {
        /*
        泛型类<类型实参> 变量名; // 定义一个泛型类引用
        new 泛型类<类型实参>(构造方法实参); // 实例化一个泛型类对象
        泛型只能接受类，所有的基本数据类型必须使用包装类！
        * */
        MyArray<Integer> myArray = new MyArray<Integer>();//后面一个<>中可写可不写包装类
        myArray.setPos(0,10);
        myArray.setPos(1,20);
        //myArray.setPos(2,"hehe");//自动类型检查
        int a = myArray.getVal(0);//自动类型转换
        int b = myArray.getVal(1);
        System.out.println(a);
        System.out.println(b);
        System.out.println("--------");
        MyArray<String> myArray1 = new MyArray<>();
        myArray1.setPos(0,"hello");
        myArray1.setPos(1,"world");
        String str1 = myArray1.getVal(0);
        String str2 = myArray1.getVal(1);
        System.out.println(str1);
        System.out.println(str2);
    }
}
/*class MyArray{
    private Object[] objects = new Object[10];
    public Object getPos(int pos){
        return objects[pos];
    }
    public void setVal(int pos,Object val) {
        this.objects[pos] = val;
    }
}
public class Test1 {
    public static void main(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setVal(0,10);
        myArray.setVal(1,"hello");
        String str = (String)myArray.getPos(1);//必须将Object强转为String
        System.out.println(str);
    }
}*/
